import 'dart:convert';
import 'dart:io';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';

class ImageUtil {
  ///加载assets下的图片
  static String getImgPath(String name, {String format: 'png'}) {
    return 'assets/images/$name.$format';
  }

  static imageSize(String path, ImageStreamListener listener) {
    Image image = Image.file(File.fromUri(Uri.parse(path)));
    image.image.resolve(ImageConfiguration()).addListener(listener);
  }

  ///通过图片路径将图片转换成Base64字符串
  static Future<String> image2Base64(String path) async {
    File file = new File(path);
    List<int> imageBytes = await file.readAsBytes();
    return base64Encode(imageBytes);
  }

  static Future<String> image2Base64Compress(String path) async {
    if (!File(path).existsSync()) {
      return null;
    }
    var result = await FlutterImageCompress.compressWithFile(
      path,
      minWidth: 640,
      minHeight: 480,
      quality: 20,
    );
    return base64Encode(result);
  }

  // /*
  // * Md5加密
  // * */
  // static String generateMd5(String data) {
  //   var content = new Utf8Encoder().convert(data);
  //   var digest = md5.convert(content);
  //   return digest.toString();
  // }

  /*
  * Base64加密
  */
  static String encodeBase64(String data) {
    var content = utf8.encode(data);
    var digest = base64Encode(content);
    return digest;
  }

  /*
  * Base64解密
  */
  static String decodeBase64(String data) {
    return String.fromCharCodes(base64Decode(data));
  }
}
